package cn.laixueit.controller;

import cn.laixueit.base.BaseController;
import cn.laixueit.base.R;
import cn.laixueit.pojo.Book;
import cn.laixueit.pojo.BookChapter;
import cn.laixueit.pojo.BookChapterContent;
import cn.laixueit.service.BookService;
import cn.laixueit.vo.BookVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author duxiang
 * @date 2023/11/29 9:39
 */

@RestController
@RequestMapping("/book")
@CrossOrigin
public class BookController extends BaseController {
    @Resource
    private BookService bookService;

    @GetMapping("list")
    public R list(@RequestBody BookVo bookvo){
        return bookService.listBookAndOneChapters(bookvo);
    }

    @PostMapping("page/{page}/{size}")
    public R page(@PathVariable(name = "page")Integer page,
                  @PathVariable(name = "size")Integer size,
                  @RequestBody BookVo bookvo){
        Page<Book> pageInfo = new Page<>(page, size);
        return bookService.listPage(pageInfo,bookvo);
    }

    @PostMapping("/save")
    public R save(@RequestBody Book book){
        bookService.saveOrUpdateBook(book);
        return R.ok();
    }

    @PostMapping("/saveAndChapter")
    public R saveAndChapter(@RequestBody Book book){
        bookService.saveAndChapter(book);
        return R.ok();
    }

    @GetMapping("/getRow/{id}")
    public R getRow(@PathVariable("id") String id){
        Book book = bookService.getById(id);
        return R.ok().put("obj",book);
    }

    /**
     *
     * @param id 书籍id
     * @param chapterId 章节id
     * @return
     */
    @GetMapping("/getRowAndChapter/{id}")
    public R getRowAndChapter(@PathVariable("id") String id,@RequestParam(required = false) String chapterId){
        BookVo book = bookService.getRowAndChapter(id,chapterId);
        return R.ok().put("obj",book);
    }

    @GetMapping("/getContent/{id}")
    public R getContent(@PathVariable("id") String id){
        BookChapterContent book = bookService.getContent(id);
        return R.ok().put("obj",book);
    }

    @PostMapping("/saveContent")
    public R saveContent(@RequestBody BookChapterContent chapterContent){
        bookService.saveContent(chapterContent);
        return R.ok().put("msg","成功");
    }

    @DeleteMapping("/removeChapter/{id}")
    public R removeChapter(@PathVariable("id") String id){
        bookService.removeChapterAndContent(id);
        return R.ok().put("msg","成功");
    }

    @PostMapping("/saveChapter")
    public R saveChapter(@RequestBody BookChapter chapter){
        BookChapter bookChapter = bookService.saveChapter(chapter);
        return R.ok().put("obj",bookChapter);
    }

}
